<template>
  <div class="homeAdvertise">
    <div class="homeAdvertise_box">
      <div class="homeAdvertise_top">
        <div class="homeAdvertise_title">
          <el-icon><Search /></el-icon>
          <span class="homeAdvertise_title_content">筛选搜索</span>
        </div>
        <div class="homeAdvertise_reset_button">
          <el-button>重置</el-button>
          <el-button type="primary" @click="handlerFilter">查询搜索</el-button>
        </div>
      </div>
      <div class="homeAdvertise_bottom">
        <div class="homeAdvertise_bottom_label">
          <span class="homeAdvertise_input_label">广告名称：</span>
          <div class="homeAdvertise_input_box">
            <el-input type="text" v-model="keyword" placeholder="优惠券名称" />
          </div>
        </div>
        <div class="homeAdvertise_bottom_label">
          <span class="homeAdvertise_input_label">广告位置：</span>
          <div class="homeAdvertise_input_box">
            <el-select v-model="select" placeholder="全部" style="width: 115px">
              <el-option :value="option" />
            </el-select>
          </div>
        </div>
        <div class="homeAdvertise_bottom_label">
          <span class="homeAdvertise_input_label">到期时间：</span>
          <div class="homeAdvertise_input_box">
            <el-date-picker
              v-model="value2"
              type="date"
              placeholder="请选择时间"
              :disabled-date="disabledDate"
            />
          </div>
        </div>
      </div>
    </div>
    <div class="homeAdvertise_tianjin">
      <div class="homeAdvertise_left">
        <div class="homeAdvertise_left_icon">
          <el-icon><Tickets /></el-icon>
          <span class="homeAdvertise_left_icon_item">数据列表</span>
        </div>
      </div>
      <div class="homeAdvertise_right_btn">
        <el-button size="small" @click="handleRegister">选择品牌</el-button>
      </div>
    </div>
    <!-- 列表 -->
    <div class="homeAdvertise_table">
      <el-table
        :data="tableList"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="60" />
        <el-table-column prop="id" label="编号" />
        <el-table-column prop="name" label="广告名称" />
        <el-table-column prop="brandName" label="广告位置">
          <template #default="scope">
            <div v-if="scope.row.type === 1">app首页轮播</div>
            <div v-else>PC首页轮播</div>
          </template>
        </el-table-column>
        <el-table-column label="广告图片">
          <template #default="scope">
            <div>
              <img
                style="width: 80px; height: 80px"
                :src="scope.row.pic"
                alt=""
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="brandName" label="时间" width="250">
          <template #default="scope">
            <div>
              开始时间:{{ formatTime(scope.row.startTime) }}
              <br />
              结束时间:{{ formatTime(scope.row.endTime) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="上线/下线" width="100">
          <template #default="scope">
            <div>
              <el-switch
                v-model.number="scope.row.status"
                :active-value="1"
                :inactive-value="0"
                @change="Hometown(scope.row)"
              /></div
          ></template>
        </el-table-column>
        <el-table-column prop="clickCount" label="点击次数" width="180" />
        <el-table-column prop="orderCount" label="生成订单" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button link type="primary" size="small">编辑</el-button>
            <el-button
              link
              type="primary"
              size="small"
              @click="deleteBer(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="homeAdvertise_bartend">
        <div class="homeAdvertise_bartend_left">
          <el-select
            v-model="select"
            placeholder="批量操作"
            style="margin-right: 10px"
          >
            <el-option :value="props" />
          </el-select>
          <el-button type="primary" @click="operation">确定</el-button>
        </div>
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[5, 10, 15]"
          :small="false"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { Search, Tickets } from "@element-plus/icons-vue";
import useTable from "@/hooks/useTable";
import * as home from "@/request/modules/home";
import formatTime from "@/utils/formatTime";
import { ElMessageBox, ElMessage } from "element-plus";

const handlerFilter = () => {};
const keyword = ref("");
const props = ref("删除");
const select = ref("");
const value2 = ref("");
const size = ref<"default" | "large" | "small">("default");

const disabledDate = (time: Date) => {
  return time.getTime() > Date.now();
};

let option = ref<number[]>([]);
const handleRegister = () => {};
const handleSelectionChange = (val: []) => {
  console.log(val);
  option.value = val.map((item) => item.id);
  // console.log(JSON.parse(JSON.stringify(val)));
  // option.value = JSON.parse(JSON.stringify(val));
  console.log(option.value);
};

const operation = () => {
  if (props.value === "删除") {
    ElMessageBox.confirm("是否要执行删除操作", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(async () => {
        ElMessage({
          type: "success",
          message: "成功删除",
        });
        await home.advertisdelete(option.value);
        fetchData();
      })
      .catch(() => {});
  }
};

const {
  pagination,
  tableList,
  fetchData,
  handleSizeChange,
  handleCurrentChange,
} = useTable(home.getadvertise, {});

// 删除
const deleteBer = (row: any) => {
  ElMessageBox.confirm("是否要执行删除操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      ElMessage({
        type: "success",
        message: "成功删除",
      });
      await home.advertisdelete(row.id);
      fetchData();
    })
    .catch(() => {});
};

// 修改上线/下线状态
const Hometown = (row: any) => {
  console.log(row);
  ElMessageBox.confirm("是否要修改推荐状态?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      ElMessage({
        type: "success",
        message: "成功切换",
      });
      await home.AdvertiseUpdate({
        id: row.id,
        status: row.status,
      });
      fetchData();
    })
    .catch(() => {
      ElMessage({
        type: "success",
        message: "已取消操作",
      });
      fetchData();
    });
};
</script>

<style scoped lang="less">
.homeAdvertise {
  padding: 0 20px;
  margin-top: 40px;
  .homeAdvertise_box {
    border-radius: 4px;
    border: 1px solid #ebeef5;
    background-color: #fff;
    overflow: hidden;
    color: #303133;
    padding: 20px;
    .homeAdvertise_top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .homeAdvertise_title {
        display: flex;
        align-items: center;
      }
    }
    .homeAdvertise_bottom {
      margin-top: 15px;
      display: flex;
      align-items: center;
      .homeAdvertise_bottom_label {
        display: flex;
        align-items: center;
        vertical-align: top;
        font-size: 14px;
        margin-left: 50px;
      }
      .homeAdvertise_input_box {
        text-align: right;
        vertical-align: middle;
        float: left;
        font-size: 14px;
        color: #606266;
        line-height: 40px;
        padding: 0 12px 0 0;
        box-sizing: border-box;
      }
    }
  }
  .homeAdvertise_tianjin {
    margin: 20px 0;
    padding: 20px;
    border-radius: 4px;
    border: 1px solid #ebeef5;
    background-color: #fff;
    overflow: hidden;
    color: #303133;
    transition: 0.3s;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .homeAdvertise_left {
      .homeAdvertise_left_icon {
        display: flex;
        align-items: center;
      }
    }
  }
  .homeAdvertise_bartend {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 20px 0;
  }
}
</style>
